
import time
import random

import dexen

import individual
from libs import input_reading


class InitializationTask(dexen.Task):
    def __initialize__(self):
        self.boxes = input_reading.read_input()
        self.nboxes = len(self.boxes)
    
    def __isinput__(self, ind):
        pass
    
    def __hasinput__(self):
        return False
    
    def __isinit__(self):
        return True

    def __execute__(self):
        print "I am in initialization"
        inds = []
        initial_size = 400
        while initial_size > 0:
            ind = self.generate_individual()
            inds.append(ind)
            initial_size -= 1        
        return inds
  
    def generate_individual(self):
        ind = individual.Individual()
        genotype = {
                "gamma_x" : range(self.nboxes),
                "gamma_y" : range(self.nboxes),
                "orient" : [None] * self.nboxes,
                "encoding" : [None] * self.nboxes
        }
        ind.genotype = genotype
        random.shuffle(genotype['gamma_x'])
        random.shuffle(genotype['gamma_y'])
        for i, box_no in enumerate(genotype['gamma_x']):
            genotype['encoding'][box_no] = dict(x=i)    
        for i, box_no in enumerate(genotype['gamma_y']):
            genotype['encoding'][box_no]['y'] = i
        for i in range(self.nboxes):
            genotype['orient'][i] = random.randint(0, 1)
        return ind  

    